/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package kiosk;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Tooltip;
import queues.QueuesClientWindowController;
import static queues.QueuesClientWindowController.SUBJECT_ID_PROP;
import server.Subject;

/**
 * FXML Controller class
 *
 * @author Renato Rosa <2101076@my.ipleiria.pt>
 */
public class KioskWindowController extends QueuesClientWindowController<Kiosk> {
    
    //The Buttons
    @FXML
    private Button button1;
    @FXML
    private Button button2;
    @FXML
    private Button button3;
    @FXML
    private Button button4;
    @FXML
    private Label labelInfo;
    
    //A list to put the buttons in order and traverse them
    private List<Button> buttonsList = new ArrayList<>();
    
     /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        buttonsList.add(button1);
        buttonsList.add(button2);
        buttonsList.add(button3);
        buttonsList.add(button4);
    }
    
     public void setSubjects(final List<Subject> subjects) {
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                for (Button b : buttonsList) {
                    b.setVisible(false);
                }
                for (int i = 0; i < subjects.size(); i++) {
                    Subject a = subjects.get(i);
                    Button b = buttonsList.get(i);
                    b.setText(a.getMnemonic() + " - " + a.getName());
                    b.setTooltip(new Tooltip(a.getName()));
                    //each button knows the id of it's subject. This way, we can handle them in the same manner
                    b.getProperties().put(SUBJECT_ID_PROP, a.getId());
                    b.setVisible(true);
                }
            }
        });  
    }
    
    //Action event on Buttons, declared in FXML
    @FXML
    private void presentSubjectInfo(ActionEvent event) throws Exception {
        Button source = (Button) event.getSource();
        Integer id = (Integer) source.getProperties().get(SUBJECT_ID_PROP);
        app.presentSubjectInfo(id);
    }
    
    public void showInfo(final String message) {
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                labelInfo.setText(message);
            }
        });
    }
}
